package com.tanhua.backstage.controller;

import com.tanhua.backstage.service.ManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("manage/users")
@Slf4j
@RestController
public class ManageController {

    /**
     *  用户冻结
     *
     * @param param
     * @return
     */
    //注入managerService
    @Autowired
    private ManageService manageService;

    //根据接口文档确定路径以及参数
    @PostMapping("freeze")
    public ResponseEntity<String> manageFreeze(@RequestHeader("Authorization")String token, @RequestBody Map<String, String> param) {

        try {


            //获取接口参数
            Integer userId = Integer.valueOf(param.get("userId"));
            Integer freezingTime = Integer.valueOf(param.get("freezingTime"));
            Integer freezingRange = Integer.valueOf(param.get("freezingRange"));
            String reasonForFreezing = param.get("reasonForFreezing");
            String forzenRemarks = param.get("frozenRemarks");
            //调用service，将参数传递过去，得到boolean类型的返回值，进行判断
            String message = this.manageService.manageFreeze(userId, freezingTime, freezingRange, reasonForFreezing,
                    forzenRemarks);

            //如果返回的值不为null，则说明操作成功，将字符串进行返回
            if (message != null) {
                return ResponseEntity.ok(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果为null。则说明操作失败，返回500
        return ResponseEntity.status(500).build();
    }

    /**
     * 用户解冻
     * @param token
     * @param param
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<String> manageUnfreeze(@RequestHeader("Authorization") String token,@RequestBody Map<String, Object> param){

        try{
            //获取接口参数
            Integer userId = Integer.valueOf(param.get("userId").toString());
            String reasonsForThawing = param.get("reasonsForThawing").toString();
            //调用service，将参数传递过去，得到boolean类型的返回值，进行判断
            String message = this.manageService.manageUnfreeze(userId, reasonsForThawing);
            //如果返回的值不为null，则说明操作成功，将字符串进行返回
            if(message != null){
                return  ResponseEntity.ok(message);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //如果为null，则说明操作失败，返回500
        return ResponseEntity.status(500).build();
    }
}